import axios from 'axios'
import { Toast } from 'vant'

const ajax = axios.create({
    baseURL: 'https://shopapi.smartisan.com'
})

ajax.interceptors.request.use(config => {
    Toast.loading({
        message: '加载中...',
        forbidClick: true
    })
    return config
})
ajax.interceptors.response.use(resp => {
    Toast.clear()
    if (resp.status === 200 && resp.data.code === 0) {
        return resp.data.data
    } else {
        return 'error'
    }
})

const ajaxOrigin = axios.create()

ajaxOrigin.interceptors.request.use(config => {
    Toast.loading({
        message: '加载中...',
        forbidClick: true
    })
    return config
})
ajaxOrigin.interceptors.response.use(resp => {
    Toast.clear()
    if (resp.status === 200 && resp.data.msg === 'success') {
        return resp.data.data
    } else {
        return 'error'
    }
})

const getHomeData = () => ajax.get('mobile/new/home?channel_id=1002')
const getCategoryData = () => ajax.get('mobile/classify')
const getHomeWaterfallData = (page, pageSize) => ajax.get(`/mobile/waterfall?page=${page}&pageSize=${pageSize}`)
// const getItemData = (id) => ajax.get(`/product/skus?ids=${id}&with_stock=true&with_spu=true`)
const getItemData = (id) => ajax.get(`/product/skus?ids=${id}&with_stock=true&with_spu=true`)
const getListData = (classifyId, page) => {
    return ajaxOrigin.get(`v1/search/goods-list?type=shop&category_id=${classifyId}&num=20&sort=sort&channel_id=1002&page=${page}`)
}
const getTestData = () => {
    return ajaxOrigin.get('/v1/search/goods-list?type=shop&category_id=394&num=20&sort=sort&channel_id=1002&page=1')
}
const getHotWordsData = () => {
    return axios.get('/v1/search/hot-words')
}
const getPromotionsData = () => ajax.get('/product/promotions?with_num=true')
const getRecommendList = () => axios.get('/www_json/DefaultRecmdSku.json?_v=1596247951404')
const getItemsData = (idList) => {
    const itemIdStr = idList.join(',')
    return ajax.get(`/product/skus?ids=${itemIdStr}&with_stock=true&with_spu=true`)
}

export default {
    getHomeData,
    getCategoryData,
    getHomeWaterfallData,
    getItemData,
    getListData,
    getTestData,
    getHotWordsData,
    getPromotionsData,
    getRecommendList,
    getItemsData
}
